Java Database Programming with JDBC Java Database Programming with JDBC
by Pratik Patel
Coriolis, The Coriolis Group
ISBN: 1576100561   Pub Date: 10/01/96
  

Previous Table of Contents Next


JDBC URL And The Connection

The format for specifying a data source is an extended Universal Resource Locator (URL). The JDBC URL structure is broadly defined as follows

jdbc:<subprotocol>:<subname>

where jdbc is the standard base, subprotocol is the particular data source type, and subname is an additional specification that can be used by the subprotocol. The subname is based solely on the subprotocol. The subprotocol (which can be “odbc,” “oracle,” etc.) is used by the JDBC drivers to identify themselves and then to connect to that specific subprotocol. The subprotocol is also used by the DriverManager to match the proper driver to a specific subprotocol. The subname can contain additional information used by the satisfying subprotocol (i.e. driver), such as the location of the data source, as well as a port number or catalog. Again, this is dependent on the subprotocol’s JDBC driver. JavaSoft suggests that a network name follow the URL syntax:

jdbc:<subprotocol>://hostname:port/subsubname

The mSQL JDBC driver used in this book follows this syntax. Here’s the URL you will see in some of the example code:

jdbc:msql://mycomputer.com:1112/databasename

The DriverManager.getConnection method in the JDBC API uses this URL when attempting to start a connection. Remember that a valid driver must be registered with the JDBC DriverManager before attempting to create this connection (as I discussed earlier in the Registering and Calling JDBC Drivers section). The DriverManager.getConnection method can be passed in a Property object where the keys “user,” “password,” and even “server” are set accordingly. The direct way of using the getConnection method involves passing these attributes in the constructor. The following is an example of how to create a Connection object from the DriverManager.getConnection method. This method returns a Connection object which is to be assigned to an instantiated Connection class:

String url="jdbc:msql://mydatabaseserver.com:1112/databasename";
Name = "pratik";
password = "";
Connection con;
con = DriverManager.getConnection(url, Name, password);
// remember to register the driver before doing this!

Chapter 4 shows a complete example of how to use the DriverManager and Connection classes, as well as how to execute queries against the database server and get the results.

Using ODBC Drivers

In an effort to close the gap between existing ODBC drivers for data sources and the emerging pure Java JDBC drivers, JavaSoft and Intersolv released the JDBC-ODBC Bridge. Note that there is a Java interface (hidden as a JDBC driver called JdbcOdbcDriver and found in the jdbc/odbc/ directory below) that does the necessary JDBC to ODBC translation with the native method library that is part of the JDBC-ODBC bridge package. Although Chapter 5 covers the inner workings of the Bridge, I would like to show you how to install it here. Once the Bridge is set up, the JDBC handles access to the ODBC data sources just like access to normal JDBC drivers; in essence, you can use the same Java code with either JDBC drivers or ODBC drivers that use the Bridge—all you have to do is change the JDBC URL to reflect a different driver.

Installing The JDBC-ODBC Bridge

There are three steps to installing the JDBC-ODBC Bridge. You’ll need to get the package first. Look on the CD-ROM, or grab the latest version from JavaSoft’s Web site at http://splash.javasoft.com/jdbc.

1.  Uncompress the package.
2.  Move the jdbc directory (located in the jdbc-odbc/classes directory) into a directory listed in your CLASSPATH, or move it to your regular Java API tree.
3.  Move JdbcOdbc.dll into your java/bin directory to make sure that the system and Java executables can find the file. You can also:
For Unix:
  Add the path location of the JdbcOdbc.dll to your LD_LIBRARY_PATH, or move the DLL into a directory covered by this environment variable.

For Windows 95:
  Move the DLL into the \WINDOWS\SYSTEM directory.

Setting Up ODBC Drivers

The data sources for the ODBC driver and the drivers themselves must be configured before you can run Java programs that access them. Consult your platform documentation and ODBC server’s documentation for specific information.

One of the great features of the Bridge is that it allows you to use existing data sources to start developing database-aware Java applications. And with Access, you don’t even need a database server! In Chapter 11, I present the full source code for writing an application server that can use the JDBC-ODBC Bridge, the Access ODBC drivers that come with Access 95, and an Access database to develop Java applets that can interact with a database without having a database server.

To set up an Access database for ODBC, follow these steps (I’m assuming that you are using Windows 95):

1.  Make sure you have the Access 95 ODBC drivers installed. These ODBC drivers can be installed from the Access install program.
2.  Select Start Menu|Settings|Control Panels.
3.  Click on 32 bit ODBC.
4.  Click on the Add button and choose the Access Driver.
5.  Type in a Data Source Name and Description (anything you like).
6.  In the Database area, click on Select.
7.  Select the Access database file; a sample database is located in MSoffice\ACCESS\Samples (if you installed it during the Access installation). However, you can specify any Access database you want.
8.  You may want to click on the Advanced button and set the Username and Password. Click on OK and then on Close to complete the configuration.

That is all you need to do to set up the ODBC data source. Now you can write Java applications to interact with the data source on the machine in which you performed the configuration; the ODBC driver is not directly accessible over the network. You can access the data source by using the name you supplied in Step 5. For example, the URL would be something like

jdbc:odbc:DataSourceName

and the statement

Class.forName("jdbc.odbc.JdbcOdbcDriver")

would load the JDBC-ODBC bridge.

Summary

The next chapter works through a complete example of using a JDBC driver. I use the mSQL driver to query an mSQL database server over the network. The JDBC driver can easily be changed to use an ODBC driver or another JDBC driver to connect to a different data source.


Previous Table of Contents Next